(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.facility.updateContinueController', updateContinueController);
	updateContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'facilityService' ];
	function updateContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, facilityService) {
		var ctrl = this, facilityInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			var params = {
					facilityId : facilityInfo.facilityId
			};
			facilityService.initTree($scope, ctrl);
			facilityService.initAttr($scope, ctrl);
			
			authService.updateContinue($scope.$stateParams.params).then(function(response) {
				ctrl.facility = response.data.model || {};
				ctrl.facility.taskId = response.data.task.taskId;
				// facilityused
				if (ctrl.facility.used == 'Y') {
					angular.forEach(ctrl.facility.facilityAttrList, function(attr, index) {
						attr.used = 'Y';
					});
				}
			});
		};

		function validateAttr(ctrl) {
			if (!ctrl.facility.facilityAttrList || ctrl.facility.facilityAttrList.length == 0) {
				var messages = ["js.rms.node.alert.checkitemnull", "js.rms.facility.alert.attrnull"];
				return messages;
			}
		}
		
		ctrl.updateContinueSave = function() {
			ctrl.facility.limitNodePathList = ctrl.facility.limitNodePathList ||[];
			ctrl.facility.limitNodePaths = ctrl.facility.limitNodePathList.length == 0 ? ctrl.facility.limitNodePaths : ctrl.facility.limitNodePathList;
			var msg1 = validateAttr(ctrl );
			if (msg1 && msg1.length > 0) {
				modalService.openErrorDialogs({
					'messages' : msg1
				});
				return;
			}
			return authService.updateContinueSave(ctrl.facility).then(saveSuccessful, saveFailed);
		};

		ctrl.updateContinueSubmit = function() {
			ctrl.facility.limitNodePathList = ctrl.facility.limitNodePathList ||[];
			ctrl.facility.limitNodePaths = ctrl.facility.limitNodePathList.length == 0 ? ctrl.facility.limitNodePaths : ctrl.facility.limitNodePathList;
			var msg1 = validateAttr(ctrl );
			if (msg1 && msg1.length > 0) {
				modalService.openErrorDialogs({
					'messages' : msg1
				});
				return;
			}
			return authService.updateContinueSubmit(ctrl.facility).then(submitSuccessful, submitFailed);
		};
		
			
		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.facility.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateContinueCancel(ctrl.facility).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedLimitCategory = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.facility.list',
					'state' : 'cda.facility.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.facility = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.facility.list',
					'state' : 'cda.facility.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}		
	}
})(angular);
